﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Engine.*;

    public class IsoViewport extends Viewport {

        public static const LOADED:String = "loaded";

        protected var m_objectLayers:Array;
        private var m_optimizeCulling:Boolean = true;

        public function IsoViewport(){
            this.m_objectLayers = [];
            super();
        }
        public function addObjectLayer(_arg1:ObjectLayer):void{
            this.m_objectLayers.push(_arg1);
            this.m_objectLayers.sortOn("priority", Array.NUMERIC);
            while (objectBase.numChildren > 0) {
                objectBase.removeChildAt(0);
            };
            var _local2:int;
            while (_local2 < this.m_objectLayers.length) {
                objectBase.addChild(this.m_objectLayers[_local2].getDisplayObject());
                _local2++;
            };
        }
        public function removeObjectLayer(_arg1:ObjectLayer):void{
            var _local3:DisplayObject;
            var _local2:int = this.m_objectLayers.indexOf(_arg1);
            if (_local2 >= 0){
                this.m_objectLayers.splice(_local2, 1);
                _local3 = _arg1.getDisplayObject();
                if (((_local3) && (_local3.parent))){
                    _local3.parent.removeChild(_local3);
                };
            };
        }
        public function set optimizeCulling(_arg1:Boolean):void{
            this.m_optimizeCulling = _arg1;
        }
        override protected function drawBackground(_arg1:BitmapData):void{
            _arg1.fillRect(_arg1.rect, Config.VIEWPORT_CLEAR_COLOR);
            var _local2:World = World.getInstance();
            var _local3:TileMap = _local2.getTileMap();
            _local3.render(this);
            if (!this.m_optimizeCulling){
                _local2.updateCulling();
            };
        }
        override protected function render(_arg1:Boolean=false):Boolean{
            var _local2:Boolean = super.render(_arg1);
            if (_local2){
                World.getInstance().updateCulling();
            };
            return (_local2);
        }
        override public function centerViewport():void{
            var _local1:World = World.getInstance();
            var _local2:TileMap = _local1.getTileMap();
            this.centerOnTilePos((_local2.getWidth() / 2), (_local2.getHeight() / 2));
        }
        public function centerOnTilePos(_arg1:Number, _arg2:Number):void{
            var _local3:Vector2 = new Vector2();
            var _local4:World = World.getInstance();
            var _local5:TileMap = _local4.getTileMap();
            var _local6:Point = IsoMath.getPixelDeltaFromTileDelta(_local5.getWidth(), 0);
            var _local7:Point = IsoMath.getPixelDeltaFromTileDelta(0, _local5.getHeight());
            var _local8:Point = _local6.add(_local7);
            var _local9:int = (_arg1 - (_local5.getWidth() / 2));
            var _local10:int = (_arg2 - (_local5.getHeight() / 2));
            var _local11:Point = IsoMath.getPixelDeltaFromTileDelta(_local9, 0);
            var _local12:Point = IsoMath.getPixelDeltaFromTileDelta(0, _local10);
            var _local13:Point = _local11.add(_local12);
            if (this.stage != null){
                _local3.x = (((this.stage.stageWidth - (_local6.x - _local7.x)) / 2) - _local13.x);
                _local3.y = (((this.stage.stageHeight + _local8.y) / 2) - _local13.y);
                setScrollPosition(_local3);
            };
        }

    }
}//package Engine.Classes 
